﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Navigation;
using System.Device.Location;
using System.Windows.Media;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Maps.Controls;

using Yuanna.Model;
using GeoOffset4China;
using System.Windows.Shapes;
using System.Windows;
using System.Windows.Media.Imaging;
using System.IO;
using Microsoft.Xna.Framework.Media;
using System.IO.IsolatedStorage;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using Microsoft.Phone.Shell;

namespace Yuanna
{
    public partial class YuannaPathMap : PhoneApplicationPage
    {
        int yuannaPathId;
        GeoCoordinateCollection yuannaCoordinates = new GeoCoordinateCollection();

        public YuannaPathMap()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 进入此页面
        /// </summary>
        /// <param name="e"></param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            yuannaPathId = Convert.ToInt32(NavigationContext.QueryString["pa_id"]);

            YuannaPath yuannaPath = App.ViewModel.GetYuannaPath(yuannaPathId);
            if (yuannaPath != null)
            {
                DataContext = yuannaPath;

                var yuannaPoints = yuannaPath.Points;
                if (yuannaPoints != null && yuannaPoints.Count > 5)
                {
                    var data = yuannaPoints.Skip(5).ToList();

                    YuannaPoint firstPoint = data[0];
                    GeoCoordinate geoCoordinate = new GeoCoordinate(firstPoint.Latitude, firstPoint.Longitude);
                    yuannaMap.Center = GeoCoordinateOffset4China.GetOffsetCoordinate(geoCoordinate); // 纠偏

                    MapLayer locationLayer = new MapLayer();
                    for (int i = 0; i < data.Count(); i++)
                    {
                        var po = data[i];
                        GeoCoordinate geoCoordinate2 = new GeoCoordinate(po.Latitude, po.Longitude);
                        GeoCoordinate geoCoordinate4China = GeoCoordinateOffset4China.GetOffsetCoordinate(geoCoordinate2); // 纠偏
                        yuannaCoordinates.Add(geoCoordinate4China);

                        if (i == 0) // 起始点
                        {
                            Polygon startPolygon = new Polygon();
                            startPolygon.Stroke = new SolidColorBrush(Colors.White);
                            startPolygon.Fill = new SolidColorBrush(Colors.Green);
                            startPolygon.Points.Add(new Point(0, 0));
                            startPolygon.Points.Add(new Point(16, 0));
                            startPolygon.Points.Add(new Point(8, 30));

                            MapOverlay myLocationOverlay1 = new MapOverlay();
                            myLocationOverlay1.Content = startPolygon;
                            myLocationOverlay1.PositionOrigin = new Point(0.5, 1);
                            myLocationOverlay1.GeoCoordinate = geoCoordinate4China;
                            locationLayer.Add(myLocationOverlay1);
                        }

                        if (i == data.Count - 1) // 结束点
                        {
                            Polygon stopPolygon = new Polygon();
                            stopPolygon.Stroke = new SolidColorBrush(Colors.White);
                            stopPolygon.Fill = new SolidColorBrush(Colors.Brown);
                            stopPolygon.Points.Add(new Point(8, 0));
                            stopPolygon.Points.Add(new Point(16, 30));
                            stopPolygon.Points.Add(new Point(0, 30));

                            MapOverlay myLocationOverlay2 = new MapOverlay();
                            myLocationOverlay2.Content = stopPolygon;
                            myLocationOverlay2.PositionOrigin = new Point(0.5, 0);
                            myLocationOverlay2.GeoCoordinate = geoCoordinate4China;
                            locationLayer.Add(myLocationOverlay2);
                        }
                    }

                    yuannaMap.Layers.Add(locationLayer);

                    MapPolyline polyline1 = new MapPolyline();
                    polyline1.StrokeColor = Colors.White;
                    polyline1.StrokeThickness = 5;
                    polyline1.Path = yuannaCoordinates;
                    yuannaMap.MapElements.Add(polyline1);

                    MapPolyline polyline2 = new MapPolyline();
                    polyline2.StrokeColor = (Color)Application.Current.Resources["PhoneAccentColor"];
                    polyline2.StrokeThickness = 3;
                    polyline2.Path = yuannaCoordinates;
                    yuannaMap.MapElements.Add(polyline2);
                }
            }
        }

        private void shareAppBarButton_Click(object sender, EventArgs e)
        {
            string fileName = string.Empty;
            using (var myStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                string dirPath = "Yuanna";
                fileName = System.IO.Path.Combine(dirPath, string.Format("WP_YUANNA_{0:yyyyMMddHHmmss}", DateTime.Now));

                if (!myStore.DirectoryExists(dirPath))
                    myStore.CreateDirectory(dirPath);

                if (myStore.FileExists(fileName))
                    myStore.DeleteFile(fileName);

                using (var myFileStream = myStore.CreateFile(fileName))
                {
                    WriteableBitmap bmp = new WriteableBitmap(YuannaPathMapPage, null);
                    Extensions.SaveJpeg(bmp, myFileStream, bmp.PixelWidth, bmp.PixelHeight, 0, 90);
                }
            }

            //WriteableBitmap bmp = new WriteableBitmap(YuannaPathMapPage, null);
            //bmp.Render(App.Current.RootVisual, null);
            //bmp.Invalidate();

            //string fileName = string.Empty;
            //using (MemoryStream stream = new MemoryStream())
            //{
            //    bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 80);
            //    stream.Seek(0, SeekOrigin.Begin);

            //    MediaLibrary library = new MediaLibrary();
            //    fileName = string.Format("WP_YUANNA_{0:yyyyMMddhhmmss}", DateTime.Now);
            //    library.SavePicture(fileName, stream);

            //    stream.Close();
            //}

            NavigationService.Navigate(new Uri("/YuannaShare.xaml?image_name=" + fileName, UriKind.Relative));
        }
    }
}